var service_user = require("../../../service/user.js");
var service_common = require("../../../service/common.js");
var service_pay = require("../../../service/pay.js");
//获取应用实例
var app = getApp();
Page({
	data: {
		userInfo: {},
		formid: "",
		time_str: "",
		first_refresh: 0,
		time_css: "display:none;",
		timeout: 0,
		photo_css: "display:none",
		slide_tip: "display:none;",
	},

	reloadUserInfoBack: function(userInfo) {
		var that = this;
		if (userInfo.wechat == "null" || userInfo.wechat == undefined) {
			userInfo.wechat = "";
		}
		this.setData({
			userInfo: userInfo
		})
		this.getUserImage();
	},
	onLoad: function(option) {
		wx.showLoading({
			title: '加载数据中',
			mask: true
		});
		var that = this;
		service_common.fetchData(
			service_common.getUrl("user.getOtherUserInfo"), {
				openid: getApp().globalData.openid,
				openid2: option.openid
			},
			function(res) {
				that.reloadUserInfoBack(res.data.data);
			},
			"GET"
		)
	},
	onShow: function() {
		this.showTime();
	},
	callPhone: function(event) {
		var phone = event.target.dataset.phone;
		if (phone != "" && this.data.userInfo.mobile_visible == 0) {
			wx.makePhoneCall({
				phoneNumber: phone,
				success: function(res) {

				}
			})
		}
	},
    // 前往官网
    goMyWeb: function(){
      wx.navigateTo({
      	url: '/pages/edit/edit'
      });  
    },
	//复制
	copy_wechat: function() {
		var that = this
		wx.setClipboardData({
			data: that.data.userInfo.email,
		})
		wx.showToast({
			title: '复制成功',
			icon: 'success',
			duration: 300
		});
	},
	//复制
	copy_email: function() {
		var that = this
		wx.setClipboardData({
			data: that.data.userInfo.wechat,
		})
		wx.showToast({
			title: '复制成功',
			icon: 'success',
			duration: 300
		});
	},
	// 查找下一个名片
	findNextCard: function() {
		var that = this;
		this.setData({
			photo_css: "display:none",
		})
		wx.showLoading({
			title: '加载数据中',
			mask: true
		})
		service_user.getFindUser(function(res) {
			that.reloadUserInfoBack(res.data.data);
		});
	},

	//喜欢
	loveCard: function() {
		var that = this;
		if (this.data.userInfo.can_love) {
			service_user.loveAdd(this.data.userInfo.openid, function(res) {
				if (res.data.code == 0) {
					wx.showToast({
						title: '点赞成功',
						mask: true,
						complete: res => {

						}
					})
				} else {
					wx.showToast({
						title: '点赞失败',
						image: "../../image/cancel.png",
						mask: true
					})
				}
			});
		} else {
			wx.showToast({
				title: '已经点赞过啦',
				image: "../../image/cancel.png",
				mask: true
			})
		}
	},

	//取消喜欢
	dislike_submit: function(e) {
		add_form_id(e.detail.formId)
		this.findNextCard()
	},

	//收藏处理
	collect_submit: function(e) {
		var that = this;
		wx.getStorage({
			key: 'FIND_SUM',
			success: function(res) {
				wx.setStorage({
					key: 'FIND_SUM',
					data: res.data + 1,
				})
				console.log(res.data)
				if (res.data <= 50) {
					var formid = e.detail.formId;
					if (that.data.userInfo.openid == getApp().globalData.openid) {
						wx.showToast({
							title: '自己不需要收藏',
							mask: true,
							complete: res => {

							}
						})
					} else if (that.data.userInfo.can_collect) {
						service_user.collectAdd(that.data.userInfo.openid, function(res) {
							if (res.data.code == 0) {
								wx.showToast({
									title: '已放入名片夹',
									mask: true,
									complete: res => {

									}
								})
							} else if (res.data.code == 10003) {
								wx.showToast({
									title: '不需要重复收藏',
									image: "../../image/cancel.png",
									mask: true,
									complete: res => {

									}
								})
							} else {
								wx.showToast({
									title: '收藏失败，请重试',
									image: "../../image/cancel.png",
									mask: true
								})
							}
						});
					} else {
						wx.showToast({
							title: '不需要重复收藏',
							image: "../../image/cancel.png",
							mask: true,
							complete: res => {

							}
						})
					}
				} else {
					wx.showModal({
						title: '提示',
						content: '每天只能收藏50个用户',
						showCancel: false
					})
				}
			}
		})
	},


	to_share_submit: function(e) {
		add_form_id(e.detail.formId)
		var pages = getCurrentPages()
		wx.switchTab({
			url: "/pages/my/my",
		})
		app.globalData.currentStatu = "open"
	},

	//点击跳转到该名片的详细信息
	viewCard: function(event) {
		var openid = event.currentTarget.dataset.openid;
		wx.navigateTo({
			url: '/pages/other/other?openid=' + openid,
		})
	},
	/**
	 * 获取展示时间
	 */
	showTime: function() {
		var that = this;
		service_user.getShowTime(function(res) {
			console.log("fuck", res)
			if (res.data.data > 0) {
				if (that.data.timeout == 0) {
					clearTimeout(t);
					timeout(that, res.data.data);
					that.data.timeout = 1;
				} else if (app.globalData.share_success == 1) {
					clearTimeout(t);
					timeout(that, res.data.data);
					that.data.timeout = 1;
				}
			}
		})
	},
	/**
	 * 开通百万曝光
	 */
	pay: function(e) {

		wx.showModal({
			title: '提示',
			content: '需要前去下载APP',
			confirmText: "前往",
			success: function(res) {
				if (res.confirm) {
					wx.navigateTo({
						url: '/pages/aApp/aApp',
					})
				}
			}
		})
	},

	/**
	 * 获取用户图片
	 */
	getUserImage: function() {
		var that = this;
		service_user.getUserImage(this.data.userInfo.openid, function(res) {
			console.log(res)
			that.setData({
				image_array: res.data.data,
			})
			if (res.data.data.length > 0) {
				that.setData({
					photo_css: ""
				})
			}
			if (res.data.data.length > 3) {
				that.setData({
					slide_tip: "",
				})
			} else {
				that.setData({
					slide_tip: "display:none;"
				})
			}
			wx.hideLoading();
		})
	},
	//图片预览
	showPhoto: function(e) {
		var id = e.currentTarget.id;
		id = parseInt(id);
		var array = [];
		for (var i = 0; i < this.data.image_array.length; i++) {
			array[i] = this.data.image_array[i].b;
		}
		wx.previewImage({
			current: array[id],
			urls: array
		})
	},
	//我想出现在此
	powerDrawer: function(e) {
		var currentStatu = e.currentTarget.dataset.statu;
		this.util(currentStatu)
	},
	util: function(currentStatu) {
		/* 动画部分 */
		// 第1步：创建动画实例   
		var animation = wx.createAnimation({
			duration: 200, //动画时长  
			timingFunction: "linear", //线性  
			delay: 0 //0则不延迟  
		});

		// 第2步：这个动画实例赋给当前的动画实例  
		this.animation = animation;

		// 第3步：执行第一组动画  
		animation.opacity(0).rotateX(-100).step();

		// 第4步：导出动画对象赋给数据对象储存  
		this.setData({
			animationData: animation.export()
		})

		// 第5步：设置定时器到指定时候后，执行第二组动画  
		setTimeout(function() {
			// 执行第二组动画  
			animation.opacity(1).rotateX(0).step();
			// 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
			this.setData({
				animationData: animation
			})

			//关闭  
			if (currentStatu == "close") {
				this.setData({
					showModalStatus: false
				});
			}
		}.bind(this), 200)

		// 显示  
		if (currentStatu == "open") {
			this.setData({
				showModalStatus: true
			});
		}
	}
})
//循环执行任务变量
var t = null
//设置倒计时
function timeout(that, time) {
	var temp = time - 1
	var time_str = getVipTime(temp)
	that.setData({
		time_str: time_str,
		time_css: "position:absolute;right:-50rpx;bottom:-75rpx;width:160rpx;height:54rpx; display:flex;display:-webkit-flex;align-items:center;justify-content: center;color:white;margin:15rpx;font-size:20rpx;font-weight:bold;"
	})
	t = setTimeout(function() {
		timeout(that, temp);
	}, 1000)
}
// 转化unix时间
function getVipTime(time) {
	var day = Math.floor(time / 3600 / 24);
	var hr = Math.floor(time / 3600 % 24);
	if (hr < 10) {
		hr = "0" + hr;
	}
	var min = Math.floor(time / 60 % 60);
	if (min < 10) {
		min = "0" + min;
	}
	var sec = Math.floor(time % 60);
	if (sec < 10) {
		sec = "0" + sec;
	}
	return day + "天 " + hr + ":" + min + ":" + sec;
}
//添加form_id
function add_form_id(form_id) {
	service_user.addFormId(form_id);
}
